﻿using System;
using System.Collections.Generic;
using VideoServices.Common.Enum;
using VideoServices.Domain.DAL;
using VideoServices.Domain.Entities;
using VideoServices.Business.BusinessException;

namespace VideoServices.Business
{
    public class StockBusinessObject
    {
        #region Fields

        private readonly IStockDataAccess stockDao;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="StockBusinessObject"/> class.
        /// </summary>
        public StockBusinessObject()
        {
            stockDao = new StockDataAccess();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StockBusinessObject"/> class.
        /// </summary>
        /// <param name="dao">The DAO.</param>
        public StockBusinessObject(IStockDataAccess dao)
        {
            // This constructor enables easy TDD as it takes in whatever IStockDataAccess implementation.
            stockDao = dao;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the stock by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Stock GetItemById(int id)
        {
            Stock stock;
            try
            {
                stock = stockDao.GetItemById(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return stock;
        }

        /// <summary>
        /// Gets the item by warehouse.
        /// </summary>
        /// <param name="warehouse">The warehouse.</param>
        /// <returns></returns>
        public IEnumerable<Stock> GetItemsByWarehouse(Warehouse warehouse)
        {
            IEnumerable<Stock> items;
            try
            {
                items = stockDao.GetItemsByWarehouse(warehouse.name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return items;
        }

        /// <summary>
        /// Creates the stock.
        /// </summary>
        /// <returns></returns>
        public Stock CreateItem(Stock newStock)
        {
            Stock stock;
            try
            {
                stock = stockDao.CreateItem(newStock);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return stock;
        }

        /// <summary>
        /// Updates the stock level.
        /// </summary>
        /// <param name="catalogue">The catalogue.</param>
        /// <param name="warehouse">The warehouse.</param>
        /// <param name="quantity">The quantity.</param>
        /// <param name="action">The action.</param>
        public void UpdateStockLevel(Catalogue catalogue, Warehouse warehouse, int quantity, StockLevelAction action)
        {
            //TODO: Implement this method
            if (action == StockLevelAction.Increment)
            {
                stockDao.IncrementStock(catalogue, warehouse, quantity);
            }
            else if (action == StockLevelAction.Decrement)
            {
                stockDao.DecrementStock(catalogue, warehouse, quantity);
            }
            else
            {
                throw new ValidationException("Stock can only be incremented or decremented.");
            }
        }

        #endregion
    }
}
